// HighlightingRulesDialog.cpp : implementation file
//

#include "stdafx.h"
#include "waveeditor.h"
#include "languagecomponents.h"
#include "HighlightingRulesDialog.h"


// HighlightingRulesDialog dialog

IMPLEMENT_DYNAMIC(HighlightingRulesDialog, CDialog)
HighlightingRulesDialog::HighlightingRulesDialog( __int64 NewMaxTime, CString NewTimeType, CWnd* pParent /*=NULL*/)
	: CDialog(HighlightingRulesDialog::IDD, pParent)
{
	MaxTime = NewMaxTime;
	TimeType = NewTimeType;
}

HighlightingRulesDialog::~HighlightingRulesDialog()
{
}

void HighlightingRulesDialog::DoDataExchange(CDataExchange* pDX)
{
	CString sTime;
	CString sPrecision;
	__int64 Value1;
	__int64 Value2;

	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_TIME_EDIT, sTime);
	DDX_Text(pDX, IDC_PRECISION_EDIT, sPrecision);

	Value1 = CStringToLong( sTime );
	Value2 = CStringToLong( sPrecision );
	InitializeMinAndMax( Value1, Value2 );
	TypeOfRule =  ( ((CButton *)GetDlgItem( IDC_OUT_OF_RANGE ))->GetCheck() == BST_CHECKED )? OUT_OF_RANGE : IN_RANGE;
}


BEGIN_MESSAGE_MAP(HighlightingRulesDialog, CDialog)
	ON_BN_CLICKED(IDC_DEL_RULE_BUTTON, OnBnClickedDeleteRules)
	ON_BN_CLICKED(IDC_MIN_MAX, OnMinMaxRadioButton)
	ON_BN_CLICKED(IDC_TIME_PRECISION, OnTimePrecisionRadioButton)
END_MESSAGE_MAP()


// HighlightingRulesDialog message handlers

BOOL HighlightingRulesDialog::OnInitDialog() 
{
    CDialog::OnInitDialog();
    ((CEdit *)GetDlgItem( IDC_TIME_EDIT ))->LimitText ( 20 );
    ((CEdit *)GetDlgItem( IDC_PRECISION_EDIT ))->LimitText ( 20 );
    ((CButton *)GetDlgItem( IDC_MIN_MAX ))->SetCheck( BST_CHECKED );
	((CButton *)GetDlgItem( IDC_OUT_OF_RANGE ))->SetCheck( BST_CHECKED );
    SetDlgItemText( IDC_STATIC1, "Min time: ("+TimeType+")" );
	SetDlgItemText( IDC_STATIC2, "Max time: ("+TimeType+")" );
  
   return TRUE;   // return TRUE unless you set the focus to a control
}

__int64 HighlightingRulesDialog::GetMin()
{
	return Min;
}

__int64 HighlightingRulesDialog::GetMax()
{
	return Max;
}

int HighlightingRulesDialog::GetTypeOfRule()
{
	return TypeOfRule;
}

void HighlightingRulesDialog::OnBnClickedDeleteRules()
{
	EndDialog( ID_DEL_RULES );
}

void HighlightingRulesDialog::OnMinMaxRadioButton()
{
	SetDlgItemText( IDC_STATIC1, "Min time: ("+TimeType+")" );
	SetDlgItemText( IDC_STATIC2, "Max time: ("+TimeType+")" );
}
void HighlightingRulesDialog::OnTimePrecisionRadioButton()
{
	SetDlgItemText( IDC_STATIC1, "Time: ("+TimeType+")" );
	SetDlgItemText( IDC_STATIC2, "Precision: ("+TimeType+")" );
}

void HighlightingRulesDialog::InitializeMinAndMax( __int64 Value1, __int64 Value2 )
{
	if ( ((CButton *)GetDlgItem( IDC_TIME_PRECISION ))->GetCheck() == BST_CHECKED )
	{
		Min = Value1 - Value2;
		Max = Value1 + Value2;
	}
	else if ( Value1 > Value2 )
	{
		Min = Value2;
		Max = Value1;
	}
	else
	{
		Min = Value1;
		Max = Value2;
	}
	NormalizeMinAndMax();
}

void HighlightingRulesDialog::NormalizeMinAndMax()
{
	Min = IntervalCompare( Min, 0, MaxTime );
	Max = IntervalCompare( Max, 0, MaxTime );
}
